﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;

    [ScriptType("SP.FieldCalculated", ServerTypeId="{f9ee4627-2914-46cd-806a-4921f96a0c72}")]
    public class FieldCalculated : Field
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public FieldCalculated(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag)
            {
                string str = peekedName;
                if (str == null)
                {
                    return flag;
                }
                if (!(str == "Formula"))
                {
                    if (str == "OutputType")
                    {
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["OutputType"] = reader.ReadEnum<FieldType>();
                        return flag;
                    }
                    if (str == "DateFormat")
                    {
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["DateFormat"] = reader.ReadEnum<DateTimeFieldFormatType>();
                    }
                    return flag;
                }
                flag = true;
                reader.ReadName();
                base.ObjectData.Properties["Formula"] = reader.ReadString();
            }
            return flag;
        }

        [Remote]
        public DateTimeFieldFormatType DateFormat
        {
            get
            {
                base.CheckUninitializedProperty("DateFormat");
                return (DateTimeFieldFormatType) base.ObjectData.Properties["DateFormat"];
            }
            set
            {
                base.ObjectData.Properties["DateFormat"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "DateFormat", value));
                }
            }
        }

        [Remote]
        public string Formula
        {
            get
            {
                base.CheckUninitializedProperty("Formula");
                return (string) base.ObjectData.Properties["Formula"];
            }
            set
            {
                if ((base.Context.ValidateOnClient && (value != null)) && (value.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("value");
                }
                base.ObjectData.Properties["Formula"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "Formula", value));
                }
            }
        }

        [Remote]
        public FieldType OutputType
        {
            get
            {
                base.CheckUninitializedProperty("OutputType");
                return (FieldType) base.ObjectData.Properties["OutputType"];
            }
            set
            {
                if (((base.Context.ValidateOnClient && (value != FieldType.Text)) && ((value != FieldType.Number) && (value != FieldType.Integer))) && (((value != FieldType.Currency) && (value != FieldType.Boolean)) && (value != FieldType.DateTime)))
                {
                    throw ClientUtility.CreateArgumentException("value");
                }
                base.ObjectData.Properties["OutputType"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "OutputType", value));
                }
            }
        }
    }
}

